<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="onConfirm"
    @cancel="$emit('close')"
  />
</template>

<script>
import dayJs from 'dayjs'
import { setUserInfo } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  props: ['value'],
  data () {
    return {
      minDate: new Date(1900, 1, 1),
      maxDate: new Date(),
      currentDate: null
    }
  },

  created () {
    this.currentDate = new Date(this.value)
  },

  methods: {
    async onConfirm (value) {
      const birthday = dayJs(value).format('YYYY-MM-DD')
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await setUserInfo({ birthday })
        this.$emit('input', birthday)
        this.$emit('close')
        Toast.success()
      } catch (err) {
        Toast.fail()
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
